import {
    updateCompanySupplier,
    deleteCompanySupplier,
    getInsuranceCompanyInfo,
    getqueryuntreatedcountapi,
    getProjectList
} from '../../api/app.js'
import user from '../../utils/user.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        projectCount: '',
        onjobnum: 0,
        popdelflag: false,
        companyId: '',
        companyName: '',
        contacts: '',
        phone: '',
        isFlow: '',
        isNeedIdcardPhoto: true,
        isSuperAdmin: false,
        contactsId: '', // 负责人id
        isAdmin: 0,
        isOpenAdvance: '',
        isShowContacts: '',
        isConfigAdvance: false,
        longJobList: [],
        tagList: [],
        approvalProcessList: [],
        isup: false,
        isupPriceLimit: false,
        innerlist: [],
        innerpagenum: 1,
        innerhasnext: true,
        eventChannel: null,
        priceLimieList: [{
            text: '不限制',
            value: 1
        }, {
            text: '自定义',
            value: 2
        }],
        priceLimitStatus: 1,
        priceLimitNum: '',
        paymentAmountCap: '',
        paymentAmountCapCopy: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        this.data.eventChannel = this.getOpenerEventChannel()
        console.log(options)
        let res = await getInsuranceCompanyInfo({
            id: options.id
        })
        console.log(res, 'fsygsuygyhdsuhuy')
        let strLong = res.payServeCompanyEnsureDetailVO.industryName !== null ? res.payServeCompanyEnsureDetailVO.industryName + ',' : ''
        let strTyx = res.payServeCompanyEnsureDetailVO.industryNameTYX !== null ? res.payServeCompanyEnsureDetailVO.industryNameTYX : ''
        let tagList = (strLong + strTyx) === '' ? [] : (strLong + strTyx).split(',')
        this.setData({
            approvalProcessList: res.approvalProcessList,
            tagList: tagList,
            longJobList: res.payServeCompanyEnsureDetailVO?.occupationList !== null ? res.payServeCompanyEnsureDetailVO?.occupationList : [],
            projectCount: res.projectCount,
            isConfigAdvance: options.isOpenAdvance === '1' ? true : false,
            isOpenAdvance: options.isOpenAdvance === '1' ? true : false,
            isShowContacts: options.isShowContacts === '1' ? true : false,
            companyId: options.id,
            companyName: options.company,
            contacts: options.contacts,
            contactsId: options.contactsId,
            paymentAmountCap: options.paymentAmountCap ? options.paymentAmountCap : '',
            phone: options.phone,
            isFlow: Number(options.isFlow),
            isNeedIdcardPhoto: Number(options.needPhoto),
            isSuperAdmin: user.isSuperAdmin,
            isAdmin: user.isAdmin
        })
    },
    onChangePriceLimitStatus(e) {
        let value = e.currentTarget.dataset.value
        console.log(value,'asfasfas');
        this.setData({
            priceLimitStatus: value
        })
        if (value === 2) {
            this.setData({
                paymentAmountCapCopy: ''
            })
        }else{
            this.setData({
                paymentAmountCapCopy: '-1'
            })
        }
    },
    onCancel() {
        this.setData({
            isupPriceLimit: false
        })
    },
    onChangeSure() {
        if (!this.data.paymentAmountCapCopy) {
            wx.showToast({
                title: '请输入限制金额',
                icon: 'none'
            })
            return
        }
        this.setData({
            paymentAmountCap: this.data.paymentAmountCapCopy,
            isupPriceLimit: false
        })
        this.onSubmit(false)
    },
    opupopup(e) {
        this.data.innerlist = []
        this.data.innerpagenum = 1
        this.data.innerhasnext = true
        if (e.currentTarget.dataset.count * 1 === 0) {
            wx.showToast({
                title: '暂无项目',
                icon: 'none'
            })
            return
        } else {
            // 获取列表
            this.getlist()
            this.setData({
                isup: true
            })
        }
    },
    onPriceLimitPopOpen() {
        this.setData({
            isupPriceLimit: true,
            paymentAmountCapCopy: this.data.paymentAmountCap,
            priceLimitStatus: this.data.paymentAmountCap === '-1' ? 1 : 2
        })
    },
    closepopup() {
        this.setData({
            isup: false
        })
    },
    closepopupPriceLimit() {
        this.setData({
            isupPriceLimit: false
        })
    },
    naexpage() {
        console.log('下一页')
        if (this.data.innerhasnext) {
            this.data.innerpagenum += 1
            this.getlist()
        }
    },
    async getlist() {
        let res = await getProjectList({
            companyId: user.companyId,
            serveCompanyId: this.data.companyId,
            pageSize: 10,
            pageNum: this.data.innerpagenum
        })
        this.setData({
            innerlist: this.data.innerlist.concat(res.list),
            innerhasnext: res.hasNextPage
        })
    },
    copy() {
        wx.setClipboardData({
            data: 'https://web.51zidan.com',
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data)
                    }
                })
            }
        })
    },
    onSetContacts() {
        wx.navigateTo({
            url: '/pages/team-list/team-list?select=selectComplete&type=radio',
        })
    },
    onSetFlow() {
        wx.navigateTo({
            url: '/pages/flow/set/set?id=' + this.data.companyId
        })
    },
    changeFlow() {
        this.setData({
            isFlow: 1,
        })
    },
    // 回调
    selectComplete(contacts) {
        console.log(contacts)
        this.setData({
            contacts: contacts[0].name,
            phone: contacts[0].phone,
            contactsId: contacts[0].id,
        })
    },
    // 提交修改
    onSubmit(isBack = true) {
        console.log()
        updateCompanySupplier({
            id: this.data.companyId,
            contacts: this.data.contacts,
            phone: this.data.phone,
            isNeedIdcardPhoto: Number(this.data.isNeedIdcardPhoto),
            contactsId: this.data.contactsId,
            isOpenAdvance: this.data.isOpenAdvance ? 1 : 0,
            isShowContacts: this.data.isShowContacts ? 1 : 0,
            paymentAmountCap: this.data.paymentAmountCap
        }).then(() => {
            app.toast('修改成功', 'success')
            if (isBack) {
                setTimeout(() => {
                    wx.navigateBack()
                }, 1500)
            }
        })
    },
    // 删除该企业
    onDelete() {
        getqueryuntreatedcountapi({
            serveCompanyId: this.data.companyId
        }).then((res) => {
            this.setData({
                onjobnum: res
            })
            console.log(res)
            if (res === 0) {
                app.confirm('是否确定删除该企业').then(() => {
                    deleteCompanySupplier({
                        id: this.data.companyId
                    }).then(() => {
                        app.toast('删除成功', 'success')
                        setTimeout(() => {
                            wx.navigateBack()
                            this.data.eventChannel.emit('getlist')
                        }, 1000)
                    })
                })
            } else {
                this.setData({
                    popdelflag: true
                })
            }
        })

    },
    // 修改是否需要员工身份证
    onChangeNeedCardPhoto(event) {
        this.setData({
            isNeedIdcardPhoto: event.detail.value
        })
    },
    onChangeisOpenAdvance(event) {
        this.setData({
            isOpenAdvance: event.detail.value
        })
    },
    closepopup1() {
        this.setData({
            popdelflag: false
        })
    },
    closepopup2() {
        this.setData({
            popdelflag: false
        })
        app.confirm('是否确定删除该企业').then(() => {
            deleteCompanySupplier({
                id: this.data.companyId
            }).then(() => {
                app.toast('删除成功', 'success')
                setTimeout(() => {
                    wx.navigateBack()
                }, 1000)
            })
        })
    },
    toInfo(e) {
        console.log(e.currentTarget.dataset.type)
        let type = e.currentTarget.dataset.type
        let id = e.currentTarget.dataset.id
        if (type === 5) {
            // 日日保
            wx.navigateTo({
                url: '/packageInsurance/pages/insurance-insure-detail/insurance-insure-detail?id=' + id + '&showmore=' + true,
            })
        } else if (type === 6) {
            // 团意保
            wx.navigateTo({
                url: '/packageInsurance/pages/insurance-insure-detail/insurance-insure-detail?id=' + id + '&showmore=' + true,
            })
        } else {
            // 发薪
            wx.navigateTo({
                url: '/packagePay/pages/new-resident/new-resident?id=' + id,
            })
        }
    },
    onChangeisShowContacts(event) {
        this.setData({
            isShowContacts: event.detail.value
        })
    },
    toprivel() {
        if (this.data.isOpenAdvance) {
            wx.navigateTo({
                url: '/pages/previewAdvancegz/previewAdvancegz',
            })
        } else {
            return
        }
    },
})